﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Base;
using NNS.Framework;
using NNS.UI;
using QLTB.BusinessLayer;
using QLTB.Common;
using QLTB.Nhom.Entity;

namespace QLTB.Nhom.UI
{
    [System.ComponentModel.ToolboxItem(false)]
    public partial class ucDSNhom : MyUIBase
    {
        public ucDSNhom()
        {
            InitializeComponent();
            dsNhom.TBL_nhom.TBL_nhomRowDeleting += TBL_nhom_TBL_nhomRowDeleting;
            dsNhom.TBL_nhom.TBL_nhomRowChanging += TBL_nhom_TBL_nhomRowChanging;
            Nap();
        }

        public event EventHandler<NhomEventArgs> NhomChanged;
        public event EventHandler<NhomEventArgs> Choosed;

        private void OnChoosed(NhomEventArgs e)
        {
            EventHandler<NhomEventArgs> choosed = Choosed;
            if (choosed != null) choosed(this, e);
        }

        private void OnNhomChanged(NhomEventArgs e)
        {
            EventHandler<NhomEventArgs> _handler = NhomChanged;
            if (_handler != null) _handler(this, e);
        }


        private void TBL_nhom_TBL_nhomRowChanging(object sender, dsNhom.TBL_nhomRowChangeEvent e)
        {
            if (e.Action == DataRowAction.Change)
            {
                if (!TBL_quyenBL.isThaydoi(EnumComponent.iPermision))
                {
                    NotSave = false;
                    Nap();
                }
            }

            //if (e.Action == DataRowAction.Add)
            //{
            //    //if (!TBL_quyenBL.isTaomoi(NNS.Enumeration.EnumComponent.iPermision))
            //    //{
            //    //    NotSave = false;
            //    //    Nap();
            //    //}
            //}
            //throw new NotImplementedException();
        }

        private void TBL_nhom_TBL_nhomRowDeleting(object sender, dsNhom.TBL_nhomRowChangeEvent e)
        {
            if (e.Action == DataRowAction.Delete)
            {
                if ((int) e.Row[dsNhom.TBL_nhom.idColumn, DataRowVersion.Original] == 0)
                {
                    ShowWarning("Nhóm người dùng này không được xóa!", "Quản nhóm người dùng");
                    NotSave = false;
                    Nap();
                }
            }
        }


        public void Nap()
        {
            dsNhom.TBL_nhom.Rows.Clear();

            SqlHelper.Helper.Fill(CommandType.Text, "SELECT id, tennhom FROM  TBL_nhom ORDER BY ID",
                                  dsNhom.TBL_nhom);
        }


        public override void Save()
        {
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            Enabled = false;
            try
            {
                tBL_nhomTableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_nhomTableAdapter.Update(dsNhom.TBL_nhom);
            }
            catch (Exception ex)
            {
                ShowWarning(ex.Message, "Quản lý nhóm người dùng.");
            }
            Enabled = true;
            Cursor.Current = current;
        }

        private void gbList_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
        }

        private void gbList_RowUpdated(object sender, RowObjectEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Save();
            Nap();
            Cursor.Current = Cursors.Default;
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Deleted();

                //Cursor.Current = Cursors.WaitCursor;
                //Save();
                //Nap();
                //Cursor.Current = Cursors.Default;
            }
        }

        public void Deleted()
        {
            if (!TBL_quyenBL.isThaydoi(EnumComponent.iPermision)) return;

            if (MyBox.ShowQuestion("Bạn thật sự muốn xóa không?", "Quản lý thiết bị") == DialogResult.No) return;

            if (gbList.SelectedRowsCount > 0)
            {
                gbList.DeleteSelectedRows();
            }
            else
            {
                gbList.DeleteRow(gbList.FocusedRowHandle);
            }

            gbList_RowUpdated(null, null);
        }

        private void gbList_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            object ob = gbList.GetRowCellValue(e.FocusedRowHandle, "id");
            if (ob == null) return;
            var nhom = new NhomEventArgs();
            nhom.Id = (int) ob;
            ob = gbList.GetRowCellValue(e.FocusedRowHandle, "tennhom");
            if (ob == null) return;
            nhom.Tennhom = (string) ob;
            OnNhomChanged(nhom);
        }

        private void iAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
        }

        private void gbList_Click(object sender, EventArgs e)
        {
            OnChoosed(new NhomEventArgs {Id = 1});
        }
    }

    public class NhomEventArgs : EventArgs
    {
        public int Id { get; set; }
        public string Tennhom { get; set; }
    }
}